var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FileManagerPlugin = require('filemanager-webpack-plugin')
const { resolve } = require('path')

var NODE_ENV = process.env.NODE_ENV
var apiUrl = ''
switch (NODE_ENV) {
  case 'dev':
    apiUrl = '"http://openapi.devch.qiaofangcloud.cn/channel-openapi"'
    break
  case 'test':
    apiUrl = '"http://openapi.devch.qiaofangcloud.cn/channel-openapi"'
    break
  case 'uat':
    apiUrl = '"http://apich.qfstatic.com/channel-openapi"'
    break
  case 'prd':
    apiUrl = '"http://qiaoxin.apich.qfstatic.com/channel-openapi"'
    break
  default:
    // apiUrl =  '"http://192.168.181.170:8088/channel-openapi"'
    apiUrl = '"http://apich.qfstatic.com/channel-openapi"'
    // apiUrl = '"http://openapi.devch.qiaofangcloud.cn/channel-openapi"'
    break
}

module.exports = {
  entry: './src/main.js',
  output: {
    path: process.env.NODE_ENV === 'development' ? path.resolve(__dirname, '') : path.resolve(__dirname, 'dist'),
    publicPath: '/',
    // filename: 'build-[hash:10].js'
    filename:  process.env.NODE_ENV === 'development' ? 'build-[hash:10].js' : '[name].[chunkhash].js'
  },  
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            scss: 'vue-style-loader!css-loader!sass-loader',
            sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }
        }
      },
      {
        test: /config.js$/,
        loader: 'webpack-replace',
        query: {
          search: 'APIURLCONFIG',
          replace: apiUrl
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          presets: ['es2015']
        },
        exclude: /node_modules/
      },
      {
        test: /\.styl$/,
        exclude: /node_modules/,
        loaders: ['style-loader', 'css-loader', 'stylus-loader']
      },
      {
        test: /\.css$/,
        loaders: ['style-loader', 'css-loader', 'stylus-loader']
      },
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader?limit=10000&minetype=application/font-woff'
      },
      {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'file-loader'
      },
      {
        test: /\.(png|jpg)$/,
        loader: 'url-loader?limit=8192' // 内联的base64的图片地址，图片要小于8k，直接的url的地址则不解析
      },
      {
        test: /\.less$/,
        //include: paths.appSrc,
        use: [
          {
            loader: 'style-loader' // creates style nodes from JS strings
          },
          {
            loader: 'css-loader' // translates CSS into CommonJS
          },
          {
            loader: 'less-loader',
            options: { javascriptEnabled: true } // compiles Less to CSS
          }
        ]
      }
      /*,
        {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
        }*/
    ]
  },
  resolve: {
    alias: {
      vue$: 'vue/dist/vue.common.js',
      '@':path.resolve(__dirname, 'src')
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#cheap-module-eval-source-map',
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, 'index.html'),
      inject: true,
    })
  ]
}

if (process.env.NODE_ENV === 'development') {
  module.exports.devServer = {
    host: '127.0.0.1',
    historyApiFallback: true,
    noInfo: true
  }
} else {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: false,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    new FileManagerPlugin({
      onEnd: {
        copy: [
          { source: 'auth_v10.html', destination: 'dist' },
          // { source: 'index.html', destination: 'dist' },
          { source: 'src/assets', destination: 'dist/src/assets' }
        ],
        archive: [{ source: 'dist', destination: 'channel-developers.zip' }]
      }
    })
  ])
}
